﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using ChineseSchool.Models.Hethong;
using System.Data.SqlClient;
using System.Data;
using ChineseSchool.Models;

namespace ChineseSchool.BLL
{
    public class B_CamNhan
    {
        private PetaPoco.Database db;
        public B_CamNhan()
        {
            db = new PetaPoco.Database("ConnectionString");
        }

        // Select All
        public DataTable GetAll()
        {
            var dt = db.ExecuteReader("EXEC CamNhan_Get_List");
            return dt;
        }

        public List<M_CamNhan> GetAlls(int Count)
        {
            var CamNhan = db.Fetch<M_CamNhan>(@";EXEC CamNhan_Get_List @@ResultCount = @0", Count);
            return CamNhan;
        }

        public ObjectWithPaging<M_CamNhan> GetAllWithPaging(int pageIndex, int pageSize)
        {
            var result = db.Page<M_CamNhan>(pageIndex, pageSize, "SELECT * FROM CamNhan");

            ObjectWithPaging<M_CamNhan> activities = new ObjectWithPaging<M_CamNhan>();
            
                activities.CurrentPage= result.CurrentPage;
                
                activities.TotalPages = result.TotalPages ;
                activities.TotalItems = result.TotalItems ;
                activities.Items = result.Items;

            return activities;
        }

        public M_CamNhan GetByPK(int CamNhanID)
        {
            var loadCamNhan = db.SingleOrDefault<M_CamNhan>("SELECT * FROM CamNhan m WHERE m.CamNhanID = " + CamNhanID + ";");
            db.CloseSharedConnection();
            return loadCamNhan;
        }

        public List<M_CamNhan> GetLastestCamNhan()
        {
            var strSql = @"SELECT TOP 10 CamNhanID, CamNhanTitle, CamNhanDate
                            FROM CamNhan
                            ORDER BY CamNhanDate DESC";
            var lstTopUser = db.Fetch<M_CamNhan>(strSql);
            return lstTopUser;
        }
    }
}